package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * 这个是专门用来映射Ajax请求的
 */
@Slf4j
@RestController
public class ajaxController {
    @Autowired
    UserMapper userMapper;

    @Autowired
    StudentMapper studentMapper;

    @RequestMapping("/loginCheck")
    public String loginCheck(@RequestParam("username") String username,
                             @RequestParam("password") String password,
                             HttpSession session){
        log.info(username+"已经登录");
        User userByUsername = userMapper.getUserByUsername(username);
        if(userByUsername == null){
            return "用户账号不存在";
        }else{
           if(userByUsername.getPassword().equals(password)){
               session.setAttribute("user",userByUsername);
               return "登录成功";

           }else{
               return"密码错误";
           }
        }

    }




    @RequestMapping("/updateStudentById")
    public int updateStudentById(@RequestParam("id") int id,
                                    @RequestParam("field") String field,
                                    @RequestParam("value") String value){
        return studentMapper.updateStudentById(id, field, value);
    }
}
